Limit CPU Usage
2013/06/21 |
Limit CPU Usage with CPULimit.
|
|
[1] | Install CPULimit |
[root@dlp ~]# yum --enablerepo=epel -y install cpulimit # install from EPEL
|
[2] | Create a program to test CPULimit. Be careful! : This program loops endlessly and uses Max CPU usage, so test it on a test machine. |
[root@dlp ~]#
vi test.sh
#!/bin/bash
while :; do :; done; chmod 700 test.sh [root@dlp ~]# ./test.sh & [1] 1409 [root@dlp ~]# top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1409 root 20 0 103m 1256 1084 R 99.1 0.1 0:12.86 test.sh 1 root 20 0 19228 1464 1184 S 0.0 0.1 0:00.46 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd # test.sh uses over 99% CPU usage like above. |
[3] | Limit CPU usage with CPULimit. |
# limit to 20% for CPU usage of test.sh [ cpulimit -l (percentage) -p (process ID) ] [root@dlp ~]# cpulimit -l 20 -p 1409 & [4] 1410 [root@dlp ~]# top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1409 root 20 0 103m 1252 1084 R 19.8 0.1 0:34.47 test.sh 1 root 20 0 19228 1464 1184 S 0.0 0.1 0:00.46 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd # just changed to under 20% usage above.
|
[4] | Down CPULimit's process if you'd like to release limitation of CPU usage by CPULimit. |
[root@dlp ~]# ps -ef | grep cpulimit root 1410 1162 0 15:11 ttyS0 00:00:00 cpulimit -l 20 -p 1409 root 1434 1417 0 15:21 ttyS0 00:00:00 grep cpulimit[root@dlp ~]# kill 1410 Exiting... [root@dlp ~]# top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1409 root 20 0 103m 1252 1084 R 100.0 0.1 3:19.51 test.sh 1 root 20 0 19228 1464 1184 S 0.0 0.1 0:00.46 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd |